package com.songshuai.webapi.baseapi.service.impl;

import com.songshuai.webapi.baseapi.dao.YixueBaseSchoolmasterNumberMapper;
import com.songshuai.webapi.baseapi.dto.request.AddMasterNumberReqDto;
import com.songshuai.webapi.baseapi.dto.request.UpdateMasterNumberReqDto;
import com.songshuai.webapi.baseapi.model.YixueBaseSchoolmasterNumber;
import com.songshuai.webapi.baseapi.pojo.feign.request.MasterNumberListParamReqDto;
import com.songshuai.webapi.baseapi.pojo.feign.request.MasterNumberParamReqDto;
import com.songshuai.webapi.baseapi.service.SchoolMasterNumberService;
import com.songshuai.webapi.baseapi.util.RedisUtil;
import com.songshuai.webapi.baseapi.utils.AutoGenericUtil;
import com.songshuai.webapi.baseapi.utils.PropertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author:wbl
 * @Date: 2019/1/3
 */
@Service
public class SchoolMasterNumberServiceImpl implements SchoolMasterNumberService {

    @Autowired
    private YixueBaseSchoolmasterNumberMapper yixueBaseSchoolmasterNumberMapper;

    @Resource
    private RedisUtil redisUtil;

    //获取自增记录数据
    @Override
    public String queryNumberValueByParam(MasterNumberParamReqDto parmDto) {
        String redisKey = parmDto.getNumberKey() + parmDto.getNumberType();
        if(redisUtil.hasKey(redisKey)){//缓存中key存在
            long redisValue = redisUtil.incr(redisKey,1);
            YixueBaseSchoolmasterNumber entity = yixueBaseSchoolmasterNumberMapper.queryMasternumberByParam(parmDto.getNumberType(),parmDto.getNumberKey());
            if(entity != null){//数据库存在
                //更新数据库数据
                UpdateMasterNumberReqDto updateDto = new UpdateMasterNumberReqDto();
                PropertiesUtil.copyProperties(parmDto,updateDto);
                updateMasterNumberUtil(updateDto,(int)redisValue);
            } else {//数据库不存在
                //新增数据库数据
                AddMasterNumberReqDto addDto = new AddMasterNumberReqDto();
                PropertiesUtil.copyProperties(parmDto,addDto);
                addMasterNumberUtil(addDto,parmDto.getLoginId(),(int)redisValue);
            }
            return redisKey + AutoGenericUtil.autoGenericCode(redisValue,parmDto.getNumLen());
        } else {//缓存中key不存在，则从数据库读取
            YixueBaseSchoolmasterNumber entity = yixueBaseSchoolmasterNumberMapper.queryMasternumberByParam(parmDto.getNumberType(),parmDto.getNumberKey());
            if(entity != null){//数据库存在
                //更新数据库数据
                UpdateMasterNumberReqDto updateDto = new UpdateMasterNumberReqDto();
                PropertiesUtil.copyProperties(parmDto,updateDto);
                updateMasterNumberUtil(updateDto,entity.getNumberValue() + 1);
                //放入缓存
                redisUtil.set(redisKey,entity.getNumberValue() + 1);
                return redisKey + AutoGenericUtil.autoGenericCode(entity.getNumberValue() + 1,parmDto.getNumLen());
            } else {//数据库不存在
                //新增数据库数据
                AddMasterNumberReqDto addDto = new AddMasterNumberReqDto();
                PropertiesUtil.copyProperties(parmDto,addDto);
                addMasterNumberUtil(addDto,parmDto.getLoginId(),parmDto.getInitNum());
                //放入缓存
                redisUtil.set(redisKey,parmDto.getInitNum());
                return redisKey + AutoGenericUtil.autoGenericCode(parmDto.getInitNum(),parmDto.getNumLen());
            }
        }
    }

    //新增工具
    private void addMasterNumberUtil(AddMasterNumberReqDto addDto,Integer loginId,Integer number){
        addDto.setCreateBy(loginId);
        addDto.setUpdateBy(loginId);
        addDto.setCreateTime(new Date());
        addDto.setUpdateTime(new Date());
        addDto.setNumberValue(number);
        addMasterNumber(addDto);
    }

    //修改工具
    private void updateMasterNumberUtil(UpdateMasterNumberReqDto updateDto,Integer number){
        updateDto.setNumberValue(number);
        updateMasterNumber(updateDto);
    }

    //新增自增记录
    @Override
    public boolean addMasterNumber(AddMasterNumberReqDto parmDto){
        YixueBaseSchoolmasterNumber entity = new YixueBaseSchoolmasterNumber();
        PropertiesUtil.copyProperties(parmDto,entity);
        return yixueBaseSchoolmasterNumberMapper.addMasterNumber(entity) > 0;
    }

    //修改自增记录
    @Override
    public boolean updateMasterNumber(UpdateMasterNumberReqDto parmDto){
        YixueBaseSchoolmasterNumber entity = new YixueBaseSchoolmasterNumber();
        PropertiesUtil.copyProperties(parmDto,entity);
        return yixueBaseSchoolmasterNumberMapper.updateMasterNumber(entity) > 0;
    }

    @Override
    public List<String> queryNumberValueListByParam(MasterNumberListParamReqDto parmDto){
        List<String> numberList = new ArrayList<>();
        String redisKey = parmDto.getNumberKey() + parmDto.getNumberType();
        if(redisUtil.hasKey(redisKey)){//缓存中key存在
            int redisValue = (int)redisUtil.get(redisKey);
            for(int i = 0;i< parmDto.getNumCount();i++){
                redisValue++;
                numberList.add(redisKey + AutoGenericUtil.autoGenericCode(redisValue,parmDto.getNumLen()));
            }
            redisUtil.set(redisKey,redisValue);
            YixueBaseSchoolmasterNumber entity = yixueBaseSchoolmasterNumberMapper.queryMasternumberByParam(parmDto.getNumberType(),parmDto.getNumberKey());
            if(entity != null){//数据库存在
                //更新数据库数据
                UpdateMasterNumberReqDto updateDto = new UpdateMasterNumberReqDto();
                PropertiesUtil.copyProperties(parmDto,updateDto);
                updateMasterNumberUtil(updateDto,redisValue);
            } else {//数据库不存在
                //新增数据库数据
                AddMasterNumberReqDto addDto = new AddMasterNumberReqDto();
                PropertiesUtil.copyProperties(parmDto,addDto);
                addMasterNumberUtil(addDto,parmDto.getLoginId(),redisValue);
            }
        } else {//缓存中key不存在，则从数据库读取
            YixueBaseSchoolmasterNumber entity = yixueBaseSchoolmasterNumberMapper.queryMasternumberByParam(parmDto.getNumberType(),parmDto.getNumberKey());
            if(entity != null){//数据库存在
                Integer numberValue = entity.getNumberValue();
                for(int i = 0;i< parmDto.getNumCount();i++){
                    numberValue++;
                    numberList.add(redisKey + AutoGenericUtil.autoGenericCode(numberValue,parmDto.getNumLen()));
                }
                //更新数据库数据
                UpdateMasterNumberReqDto updateDto = new UpdateMasterNumberReqDto();
                PropertiesUtil.copyProperties(parmDto,updateDto);
                updateDto.setNumberValue(numberValue);
                updateMasterNumber(updateDto);
                //放入缓存
                redisUtil.set(redisKey,numberValue);
            } else {//数据库不存在
                Integer numberValue = parmDto.getInitNum();
                for(int i = 0;i< parmDto.getNumCount();i++){
                    numberValue++;
                    numberList.add(redisKey + AutoGenericUtil.autoGenericCode(numberValue,parmDto.getNumLen()));
                }
                //新增数据库数据
                AddMasterNumberReqDto addDto = new AddMasterNumberReqDto();
                PropertiesUtil.copyProperties(parmDto,addDto);
                addDto.setCreateBy(parmDto.getLoginId());
                addDto.setUpdateBy(parmDto.getLoginId());
                addDto.setCreateTime(new Date());
                addDto.setUpdateTime(new Date());
                addDto.setNumberValue(numberValue);
                addMasterNumber(addDto);
                //放入缓存
                redisUtil.set(redisKey,numberValue);
            }
        }
        return numberList;
    }
}
